package com.zzptc.zzsgz.bdsjws.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.db.MobileInfoDao;
import com.zzptc.zzsgz.bdsjws.vo.Contacts;
import com.zzptc.zzsgz.bdsjws.vo.MobileInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lc on 2017/3/31.
 */

public class SelectContacts {
    public static List<Contacts> select(Context context){
        Contacts contacts;
        Cursor datacursor;
        List<Contacts> list;
        list=new ArrayList<>();
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri data= Uri.parse("content://com.android.contacts/data");
        Cursor cursor =context.getContentResolver().query(uri,new String[]{"contact_id"}, null, null, null);
        while(cursor.moveToNext()) {
            int raw_contact_id=cursor.getInt(0);
            datacursor=context.getContentResolver().query(data,new String[]{"data1","mimetype"},"raw_contact_id=?",new String[]{String.valueOf(raw_contact_id)},null);
            contacts = new Contacts();
            contacts._id=raw_contact_id;
            while (datacursor.moveToNext()) {
                String data1=datacursor.getString(0);
                String mimetype=datacursor.getString(1);
                if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
                    contacts._tel=data1;
                    contacts.address=getAddress(contacts._tel,contacts);
                }else if("vnd.android.cursor.item/name".equals(mimetype)){
                    contacts._name=data1;
                }
            }
            list.add(contacts);
        }
        return list;
    }

    private static String getAddress(String phonenum,Contacts contacts) {
        MobileInfo mobileInfo=null;
        String newtel=phonenum.replace(" ","").replace("-","");
        String telnum=null;
        if (newtel.length()==11&&newtel.matches("^1[34578]\\d{9}$")){
            telnum=newtel.substring(0,7);
            mobileInfo=MyApplication.getMobiledaoSession().getMobileInfoDao().queryBuilder()
                    .where(MobileInfoDao.Properties.MobileNumber.eq(telnum))
                    .distinct()
                    .unique();
            contacts.isEnabled=true;
        }else{
            int lenght=newtel.length();
            switch (lenght){
                case 10 :
                    mobileInfo=getMobileInfo(newtel,3);
                    break;
                case 11 :
                    mobileInfo=getMobileInfo(newtel,3);
                    String add=mobileInfo.getMobileArea();
                    if (mobileInfo==null){
                        mobileInfo=getMobileInfo(newtel,4);
                    }
                    break;
                case 12:
                    mobileInfo=getMobileInfo(newtel,4);
                    break;
                default:
            }
            contacts.isEnabled=false;
        }
        /*String talnum=phonenum.replace(" ","").replace("-","").substring(0,7);
        MobileInfo mobileinfo=MyApplication
                .getMobiledaoSession()
                .getMobileInfoDao()
                .queryBuilder()
                .where(MobileInfoDao.Properties.MobileNumber.eq(talnum))
                .distinct()
                .unique();

        return mobileinfo.getMobileArea();*/
        try {
            mobileInfo.getMobileArea();
        }catch (NullPointerException n){
            return "未知";
        }
        return mobileInfo.getMobileArea();
    }

    public static MobileInfo getMobileInfo(String newtel,int num){
        MobileInfo mobileInfo=MyApplication.getMobiledaoSession().getMobileInfoDao().queryBuilder()
                .where(MobileInfoDao.Properties.AreaCode.eq(newtel.substring(0,num))).limit(1).distinct().unique();
        return mobileInfo;
    }

}
